<?php
	
namespace App\Library;

use App\Http\Config\WechatAPI;
use Illuminate\Support\Facades\Cache;
use App\Models\Admin\Configs;
use Illuminate\Support\Facades\Mail;

//公共类
class Common{
	
	/**
	 * 邮件发送函数
	 *@ name 收件方名称
	 *@ admin 寄件方名称
	 *@ to 收件方邮箱
	 *@ info邮件内容数组 
	 *@ title邮件标题
	 * */
	public function sendMail($name,$admin,$to,$info,$tille)
    {
		
		$em1 = "wstianxia.com";
		$em2 = "wegouer.com";
		$em3 = "netbcloud.com";
		$em4 = "wangqudao.com";
		$em5 = "qcloud0755.com";
		if(strpos($to,$em1)||strpos($to,$em2)||strpos($to,$em3)||strpos($to,$em4)||strpos($to,$em5)){
			
		}else{
			return false;
		}
		
		if($tille=='业绩订单审核成功'){
			$view = 'mail.ach_success';
		}else if($tille=='业绩订单审核失败'){
			$view = 'mail.ach_fail';
		}else{
			return false;
		}
		
        $flag = Mail::send($view,['name'=>$name,'admin'=>$admin,'info'=>$info],function($message)use($to,$tille){  
            $message->to($to)->subject($tille);  
        });
      
        if(count(Mail::failures()) < 1){
            return true;
        }else{
            return false;
        }
	}

	//api更新企业微信账号
	public function setQyWechat($arr,$ccc=1)
    {
        $con = Configs::first();
        $res = file_get_contents(WechatAPI::$qywx_conf["get_token"] . '?corpid='.$con->company_id.'&corpsecret='.$con->tongxl_secret);
        $res = json_decode($res,true);
        if($ccc != 1){
            $url = WechatAPI::$qywx_conf["user_update"]."?access_token=".$res['access_token'];
        }else{
            $url = WechatAPI::$qywx_conf["user_create"]."?access_token=".$res['access_token'];
        }
        $data = json_encode($arr,JSON_UNESCAPED_UNICODE);

        $return = $this->curl($url,$data);
        return json_decode($return,true);
	}

    //api更新企业邮箱账号
    public function setQyEmail($arr,$ccc=1){
        $con = Configs::first();
        $res = file_get_contents(WechatAPI::$qywx_conf["get_token"] . '?corpid='.$con->company_id.'&corpsecret='.$con->tongxl_secret);
        $res = json_decode($res,true);
        if($ccc != 1){
            $url = WechatAPI::$qywx_conf["qyemail_create"] . "?access_token=".$res['access_token'];
        }else{
            $url = WechatAPI::$qywx_conf["qyemail_update"] . "?access_token=".$res['access_token'];
        }
        $data = json_encode($arr,JSON_UNESCAPED_UNICODE);

        $return = $this->curl($url,$data);
        return json_decode($return,true);
    }

    //api邀请用户
    public function invitationUser($arr){
        $con = Configs::first();
        $res = file_get_contents(WechatAPI::$qywx_conf["get_token"] . '?corpid='.$con->company_id.'&corpsecret='.$con->tongxl_secret);
        $res = json_decode($res,true);

        $url = WechatAPI::$qywx_conf["user_invite"] . "?access_token=".$res['access_token'];
        $data = json_encode($arr,JSON_UNESCAPED_UNICODE);

        $return = $this->curl($url,$data);
        return json_decode($return,true);
    }
	
	/**
	 * 企业微信自动推送
	 *@ user_id 被推送人企业微信ID
	 *@ content 推送内容
	 */
	public function QyWechatPush($user_id,$content){
		if(trim($user_id)==''){
			return false;
		}
		
		if(!Cache::has('xiaoxi_access_token')){
			$con = Configs::first();
			$res = file_get_contents("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".$con->company_id."&corpsecret=".$con->push_secret);
			$arr = json_decode($res,true);
			Cache::add('xiaoxi_access_token',$arr['access_token'],120);//键 值 有效时间（分钟）
		}
		
		$url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=".Cache::get('xiaoxi_access_token');
		
		$data =array(
			"touser" => $user_id,
		    "msgtype" => "text",
		    "agentid" => 1000011,
		    "text" => array(
		        "content" => $content
		    ),
		   "safe"=> 0
		);
		$data = json_encode($data);
		$res = $this->curl($url,$data);	
		if(!$res){
			return false;
		}else{
			return true;
		}
	}
	
	//curl函数
	private function  curl($url,$data){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		    'Content-Type: application/json',
		    'Content-Length: ' . strlen($data)
		));
		$output = curl_exec($ch);
		if (curl_errno($ch)) {
		    return false;
		}
		curl_close($ch);
        return $output;
    }
	
}
